package com.lahuan.config.integration;

import com.alibaba.fastjson.JSON;
import com.lahuan.config.rabbit.TopicExchangeProducer;
import com.lahuan.demo.entity.Order;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.integration.handler.GenericHandler;
import org.springframework.messaging.MessageHeaders;
import org.springframework.stereotype.Component;

/**
 * Spring Integration消息发送
 */
@Component
public class RegisterRabbitMQSendHandler implements GenericHandler<Order> {
    @Autowired
    TopicExchangeProducer producer;
    @Autowired
    RabbitTemplate rabbitTemplate;
    @Override
    public Object handle(Order order, MessageHeaders messageHeaders) {
        // 前一个Handler的返回值会是后一个Handler的参数
        // 如果最后一个handler返回了东西会报错
        // 如果中间某一个handler返回了null，就会停止后续handler
        System.out.println("发送到rabbit");
        producer.send(JSON.toJSONString(order),"test");
        //Handler最后一个要放回null，不然报错
        return order;
    }
}

